﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: TrCustomer.cs
// 作 者：代码自动生成
// 日 期：2011-11-7 10:42:35
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;
using System.Reflection;
namespace ZX.TR.Library
{
	/// <summary>
    /// 客户表
    /// </summary>
	[Serializable]
    [Table(Name="TrCustomer")]
    [ZX.DBModule.Table("TrCustomer","TrCustomerID")]
	public class TrCustomer: ZX.DBModule.BaseEntity
	{
        
        #region Business Methods 业务方法
       
		/// <summary>
        /// 客户ID
        /// </summary>
        private static PropertyInfo<Guid> TrCustomerIDProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<Guid>("TrCustomerID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid TrCustomerID
        {
            get { return GetProperty(TrCustomerIDProperty); }
            set { SetProperty(TrCustomerIDProperty, value); }
        }
        
		/// <summary>
        /// 客户编码
        /// </summary>
        private static PropertyInfo< string > CustomerCodeProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< string >("CustomerCode"));
        [Column]
        public string CustomerCode
        {
            get { return GetProperty(CustomerCodeProperty); }
            set { SetProperty(CustomerCodeProperty, value); }
        }
		/// <summary>
        /// 客户名称
        /// </summary>
        private static PropertyInfo< string > CustomerNameProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< string >("CustomerName"));
        [Column]
        public string CustomerName
        {
            get { return GetProperty(CustomerNameProperty); }
            set { SetProperty(CustomerNameProperty, value); }
        }
		/// <summary>
        /// 客户上级
        /// </summary>
        private static PropertyInfo< Guid > ParentIDProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< Guid >("ParentID"));
        [Column]
        public Guid ParentID
        {
            get { return GetProperty(ParentIDProperty); }
            set { SetProperty(ParentIDProperty, value); }
        }
		/// <summary>
        /// 客户经理
        /// </summary>
        private static PropertyInfo< Guid > PrincipalHrmannoProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< Guid >("PrincipalHrmanno"));
        [Column]
        public Guid PrincipalHrmanno
        {
            get { return GetProperty(PrincipalHrmannoProperty); }
            set { SetProperty(PrincipalHrmannoProperty, value); }
        }
		/// <summary>
        /// 公司ID
        /// </summary>
        private static PropertyInfo< Guid > CompanyIDProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< Guid >("CompanyID"));
        [Column]
        public Guid CompanyID
        {
            get { return GetProperty(CompanyIDProperty); }
            set { SetProperty(CompanyIDProperty, value); }
        }
		/// <summary>
        /// 公司编号
        /// </summary>
        private static PropertyInfo< string > CompanyCodeProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< string >("CompanyCode"));
        [Column]
        public string CompanyCode
        {
            get { return GetProperty(CompanyCodeProperty); }
            set { SetProperty(CompanyCodeProperty, value); }
        }
		/// <summary>
        /// 公司名称
        /// </summary>
        private static PropertyInfo< string > CompanyNameProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< string >("CompanyName"));
        [Column]
        public string CompanyName
        {
            get { return GetProperty(CompanyNameProperty); }
            set { SetProperty(CompanyNameProperty, value); }
        }
		/// <summary>
        /// 公司网址
        /// </summary>
        private static PropertyInfo<string> CompanyWebsiteProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<string>("CompanyWebsite"));
        [Column]
        public string CompanyWebsite
        {
            get { return GetProperty(CompanyWebsiteProperty); }
            set { SetProperty(CompanyWebsiteProperty, value); }
        }
		/// <summary>
        /// 公司邮编
        /// </summary>
        private static PropertyInfo< string > CompanyPostProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< string >("CompanyPost"));
        [Column]
        public string CompanyPost
        {
            get { return GetProperty(CompanyPostProperty); }
            set { SetProperty(CompanyPostProperty, value); }
        }
        /// <summary>
        /// 部门ID
        /// </summary>
        private static PropertyInfo<Guid> DeptIDProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<Guid>("DeptID"));
        [Column]
        public Guid DeptID
        {
            get { return GetProperty(DeptIDProperty); }
            set { SetProperty(DeptIDProperty, value); }
        }
   
       /// <summary>
        /// 部门领导
        /// </summary>
        private static PropertyInfo<string> HeadshipProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<string>("Headship"));
        [Column]
        public string Headship
        {
            get { return GetProperty(HeadshipProperty); }
            set { SetProperty(HeadshipProperty, value); }
        }
         /// <summary>
        /// 职能描述
        /// </summary>
        private static PropertyInfo<string> FunctionDescriptionProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<string>("FunctionDescription"));
        [Column]
        public string FunctionDescription
        {
            get { return GetProperty(FunctionDescriptionProperty); }
            set { SetProperty(FunctionDescriptionProperty, value); }
        }

        /// <summary>
        /// 固定电话
        /// </summary>
        private static PropertyInfo<string> TelephoneProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<string>("Telephone"));
        [Column]
        public string Telephone
        {
            get { return GetProperty(TelephoneProperty); }
            set { SetProperty(TelephoneProperty, value); }
        }
        
        /// <summary>
        /// 移动电话
        /// </summary>
        private static PropertyInfo<string> MobileProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<string>("Mobile"));
        [Column]
        public string Mobile
        {
            get { return GetProperty(MobileProperty); }
            set { SetProperty(MobileProperty, value); }
        }

        /// <summary>
        /// 邮箱地址
        /// </summary>
        private static PropertyInfo<string> EmailProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<string>("Email"));
        [Column]
        public string Email
        {
            get { return GetProperty(EmailProperty); }
            set { SetProperty(EmailProperty, value); }
        }
        
		/// <summary>
        /// 操作员ID
        /// </summary>
        private static PropertyInfo<Guid> addUserProperty = RegisterProperty(typeof(TrCustomer), new PropertyInfo<Guid>("AddUser"));
        [Column]
        public Guid AddUser
        {
            get { return GetProperty(addUserProperty); }
            set { SetProperty(addUserProperty, value); }
        }
		/// <summary>
        /// 添加时间
        /// </summary>
        private static PropertyInfo< DateTime > AddtimeProperty = RegisterProperty(typeof(TrCustomer),new PropertyInfo< DateTime >("Addtime"));
        [Column]
        public DateTime Addtime
        {
            get { return GetProperty(AddtimeProperty); }
            set { 
                DateTime dt=value==DateTime.MinValue?DateTime.Now:value;
                SetProperty(AddtimeProperty, dt); 
            }
        }
		
        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "CustomerCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CustomerCode", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "CustomerName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CustomerName", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyCode", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyName", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyWebsite");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyWebsite", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyPost");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyPost", 50));

            //base.AddBusinessRules();
        }
        #endregion
        
        #region Factory Methods 工厂方法

        public TrCustomer(){}
        /// <summary>
        /// 数据验证[]
        /// </summary>
        /// <returns>返回数据验证结果</returns>
        public override string CheckData()
        {

               string msg="";
            if (GetByDeptID(DeptID))
            {
                msg = "该公司已添加客户,不能重复添加";
            }
              return msg;


        }

        public static TrCustomer New()
        {
            return DataPortal.Create< TrCustomer>();
        }
        public static TrCustomer Get(Guid TrCustomerID)
        {
            return DataPortal.Fetch< TrCustomer>(new SingleLazyCriteria< TrCustomer, Guid>(TrCustomerID, false));
        }
        public static bool GetByDeptID(Guid deptID)
        {
            object instance = Common.Reflection.CreateInstance("ZX.TR.Bussiness.BTrcustomer", "ZX.TR.Bussiness");
            Type t = instance.GetType();
            object obj = t.InvokeMember("IsExistsByDeptID", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance, new object[] { deptID });
            return (bool)obj;
        }
        public static void Delete(Guid TrCustomerID)
        {
            DataPortal.Delete(TrCustomerID);
        }

        //public override TrCustomer Save()
        //{
            //return base.Save();
        //}
        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid TrCustomerID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
             using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("TrCustomerID").Equal(TrCustomerID);
                var data = db.Select< TrCustomer>(q)[0];
                LoadProperty(TrCustomerIDProperty, data.TrCustomerID);
                LoadProperty(CustomerCodeProperty, data.CustomerCode);
                LoadProperty(CustomerNameProperty, data.CustomerName);
                LoadProperty(ParentIDProperty, data.ParentID);
                LoadProperty(PrincipalHrmannoProperty, data.PrincipalHrmanno);
                LoadProperty(CompanyIDProperty, data.CompanyID);
                LoadProperty(CompanyCodeProperty, data.CompanyCode);
                LoadProperty(CompanyNameProperty, data.CompanyName);
                LoadProperty(CompanyWebsiteProperty, data.CompanyWebsite);
                LoadProperty(CompanyPostProperty, data.CompanyPost);

                LoadProperty(DeptIDProperty, data.DeptID);
                LoadProperty(HeadshipProperty, data.Headship);
                LoadProperty(FunctionDescriptionProperty ,data.FunctionDescription);
                LoadProperty(TelephoneProperty, data.Telephone);
                LoadProperty(MobileProperty, data.Mobile);
                LoadProperty(EmailProperty, data.Email);
                LoadProperty(addUserProperty, data.AddUser);
                LoadProperty(AddtimeProperty, data.Addtime);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(TrCustomerIDProperty);
        }
        #endregion

	}
}

